import com.tcheung.*;
import com.tcheung.entity.Document;
import com.tcheung.entity.TFEntity;
import com.tcheung.entity.TFIDFEntity;
import com.tcheung.service.IndexService;
import com.tcheung.service.QueryService;
import com.tcheung.utils.AppConstants;
import com.tcheung.utils.FileUtils;
import com.tcheung.utils.PropertiesUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.*;

/**
 * App
 *
 * @author: zhangteng
 * @time: 2014/11/30 11:42
 */
public class App {

    public static final Log log = LogFactory.getLog(App.class);

    public static void main(String[] args) {
        App.run();
    }

    public static void run() {
        // 初始化属性工具类
        PropertiesUtils propertiesUtils = new PropertiesUtils();
        // 获取属性
        String docPath = propertiesUtils.getProperty("input.home");
        String outPath = propertiesUtils.getProperty("output.home");
        String queryFilePath = propertiesUtils.getProperty("queryFile");

        /*         作业一开始              */
        Date startTime = new Date();

        // 读入所有的文档
        List<Document> documentList = WordBatchExtractor.getDocumentList(docPath);

        // 对文档进行切词
        WordSeg wordSeg = new WordSeg();
        if (documentList != null) {
            for (Document document : documentList) {
                wordSeg.segWords(document);
            }
        } else {
            log.error("Documents is null!");
            return ;
        }

        // 写入DocIndex
        try {
            FileUtils.writeToDocIndex(outPath, documentList);
        } catch (IOException e) {
            e.printStackTrace();
        }

        IndexService indexService = IndexService.getInstance(documentList);
        // 写入DocIndex(tf)
        try {
            FileUtils.writeToDocIndexTF(outPath, indexService.getTfEntityList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 写入DocIndex(tfidf)
        try {
            FileUtils.writeToDocIndexTFIDF(outPath, indexService.getTfidfEntityList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collections.sort(indexService.getTfEntityList(), new Comparator<TFEntity>() {
            @Override
            public int compare(TFEntity o1, TFEntity o2) {
                return o1.getKeyWord().compareTo(o2.getKeyWord());
            }
        });
        Collections.sort(indexService.getTfidfEntityList(), new Comparator<TFIDFEntity>() {
            @Override
            public int compare(TFIDFEntity o1, TFIDFEntity o2) {
                return o1.getKeyWord().compareTo(o2.getKeyWord());
            }
        });
        // 写入DocInvert(tf)
        try {
            FileUtils.writeToDocInvertTF(outPath, indexService.getTfEntityList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 写入DocInvert(tfidf)
        try {
            FileUtils.writeToDocInvertTFIDF(outPath, indexService.getTfidfEntityList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        Date endTime = new Date();
        System.out.println(endTime.getTime() - startTime.getTime());
        /*         作业一结束              */

        /*         作业二开始              */
        // 获取query
        QueryService queryService = QueryService.getInstance(queryFilePath);
        MRR mrr = MRR.getInstance(queryService.getQueries().size());
        Map<Integer, String> queries = queryService.getQueries();
        // 查询
        for (Map.Entry<Integer, String> entry : queries.entrySet()) {
            int queryNum = entry.getKey();
            // 计算cos值
            VSM.cosSim(documentList, queryNum);

            // 计算当前query在MRR方法中的rank
            mrr.calRank(queryNum);

            // 写入result(vectoral)结果
            try {
                FileUtils.writeToResult(outPath + "/result/before", documentList, queryNum, entry.getValue());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 写入统计结果
        try {
            // 写入答案统计结果
            FileUtils.writeToResultStatistics(outPath + "/result/before");

            // 写入mrr结果
            FileUtils.writeToMRR(outPath + "/result/before");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 查询扩展
        QueryExpansion queryExpansion = new QueryExpansion(AppConstants.ROCCHIO_A, AppConstants.ROCCHIO_B, AppConstants.ROCCHIO_C);
        // 扩展之后再查询
        for (Map.Entry<Integer, String> entry : queries.entrySet()) {
            int queryNum = entry.getKey();
            // 扩展
            queryExpansion.extend(queryNum, null);

            // 计算cos值
            VSM.cosSim(documentList, queryNum);

            // 计算当前query在MRR方法中的rank
            mrr.calRank(queryNum);

            // 写入result(vectoral)结果
            try {
                FileUtils.writeToResult(outPath + "/result/after", documentList, queryNum, entry.getValue());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 写入统计结果
        try {
            // 写入答案统计结果
            FileUtils.writeToResultStatistics(outPath + "/result/after");

            // 写入mrr结果
            FileUtils.writeToMRR(outPath + "/result/after");
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*         作业二结束              */
    }
}
